package com.health.medicine.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import com.health.api.model.producer.ProducerRegisterRequest;
import com.health.common.common.BaseResponse;
import com.health.common.common.ResultUtils;
import com.health.common.constant.UserRoleConstant;
import com.health.common.model.vo.UserKeyVO;
import com.health.medicine.model.dto.medicine.AddDrugInfoRequest;
import com.health.medicine.model.dto.producer.ProducerUpdateRequest;
import com.health.medicine.model.entity.Manufacturer;
import com.health.medicine.model.vo.ManufacturerVO;
import com.health.medicine.service.ManufacturerService;
import com.health.medicine.service.MedicineInfoService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.fisco.bcos.sdk.v3.codec.ContractCodecException;
import org.fisco.bcos.sdk.v3.transaction.model.exception.TransactionBaseException;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhexueqi
 * @ClassName ManufacturerController
 * @since 2024/8/30    16:21
 */
@RestController
@RequestMapping("/manufacturer")
public class ManufacturerController {

    @Resource
    private MedicineInfoService medicineInfoService;

    @Resource
    private ManufacturerService manufacturerService;

    /**
     * 添加生产商用户
     * @return 添加成功
     */
    @PostMapping(value = "/createManufacturerToMysql")
    public Boolean createManufacturerToMysql(@RequestBody ProducerRegisterRequest producerRegisterRequest){
        return manufacturerService.createManufacturerToMysql(producerRegisterRequest);
    }

    @PostMapping("/createManufacturer")
    @Operation(summary = "生产商注册")
    public boolean createManufacturer(@RequestBody UserKeyVO userKeyVO, @RequestParam("userAccount") String userAccount) throws TransactionBaseException, ContractCodecException {
        return manufacturerService.createManufacturer(userKeyVO,userAccount);
    }

    /**
     * 生产商将药物上链
     * @return 溯源码ID
     */
    @SaCheckLogin
    @SaCheckRole(UserRoleConstant.PRODUCER)
    @PostMapping("/products")
    @Operation(summary = "生产者将药物上链")
    public BaseResponse<Long> products(@RequestBody @Valid AddDrugInfoRequest addDrugInfoRequest) throws TransactionBaseException, ContractCodecException {
        long traceId =medicineInfoService.produce(addDrugInfoRequest);
        return ResultUtils.success(traceId);
    }

    /**
     * 生产单位更新自己的信息
     * @return 更新成功
     */
    @SaCheckLogin
    @SaCheckRole(UserRoleConstant.PRODUCER)
    @PutMapping("/updateInfo")
    @Operation(summary = "生产者更新自己的信息")
    public BaseResponse<Boolean> updateInfo(@RequestBody ProducerUpdateRequest producerUpdateRequest) {
        Boolean isSuccess = manufacturerService.updateInfo(producerUpdateRequest);
        return ResultUtils.success(isSuccess);
    }

    /**
     * 根据生产商id查询生产商信息
     * @return 生产商信息
     */
    @GetMapping("/getManufacturerInfo")
    @Operation(summary = "根据生产商id查询生产商信息")
    public BaseResponse<ManufacturerVO> getManufacturerInfo(@RequestParam("id") Long id) {
        Manufacturer manufacturer = manufacturerService.getById(id);
        ManufacturerVO manufacturerVO = new ManufacturerVO();
        BeanUtils.copyProperties(manufacturer, manufacturerVO);
        return ResultUtils.success(manufacturerVO);
    }

    /**
     * 根据id删除生产单位信息
     * @return 删除成功
     */
    @SaCheckLogin
    @SaCheckRole(UserRoleConstant.ADMIN)
    @DeleteMapping("/deleteManufacturer")
    @Operation(summary = "根据id删除生产单位信息")
    public BaseResponse<Boolean> deleteManufacturer(@RequestParam("id") Long id) {
        Boolean isSuccess = manufacturerService.removeById(id);
        return ResultUtils.success(isSuccess);
    }


}
